package com.pagoda.nerp.trade.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.pagoda.nerp.trade.common.exception.UpdateException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.pagoda.nerp.trade.common.ConstantDefine;
import com.pagoda.nerp.trade.dao.PurGoodsRequestDao;
import com.pagoda.nerp.trade.dao.SysBillRuleDao;
import com.pagoda.nerp.trade.entity.PurGoodsRequest;
import com.pagoda.nerp.trade.service.PurGoodsRequestService;

@Service
public class PurGoodsRequestServiceImpl implements PurGoodsRequestService{
	@Autowired
	private SysBillRuleDao sysBillRuleDao;
	
	@Autowired
	private PurGoodsRequestDao purGoodsRequestDao;
	
	@Override
	public List<PurGoodsRequest> selectAllByConditon(Map map) {
		return purGoodsRequestDao.selectAllByConditon(map);
	}

	@Override
	public PurGoodsRequest selectOneByCondition(Map map) {
		return purGoodsRequestDao.selectOneByCondition(map);
	}
	
//	@Transactional
	public String insert(PurGoodsRequest purGoodsRequest){
		String goodsRequestCode="";
		try {
			int goodsRequestId = sysBillRuleDao.generateId(ConstantDefine.BILLCODE_GOODS_REQUEST_ID);
			goodsRequestCode = sysBillRuleDao.generateCode(ConstantDefine.BILLCODE_GOODS_REQUEST_CODE);
			purGoodsRequest.setGoodsRequestId(goodsRequestId);
			purGoodsRequest.setGoodsRequestCode(goodsRequestCode);
			purGoodsRequest.setVendorId(0);//第一阶段买方供应商信息全部固定为百果园
			purGoodsRequest.setVendorCode("000");
			purGoodsRequest.setVendorName("百果园");
			purGoodsRequest.setStatus(ConstantDefine.PURCHASE_STATUS_INI);
			int resultCode = purGoodsRequestDao.insert(purGoodsRequest);
			if (resultCode==0) {
				goodsRequestCode="";
			}
		} catch (Exception e) {
			return "";
		}
		
		return goodsRequestCode;
	}

	@Override
	public List<PurGoodsRequest> selectLatest() {
		return purGoodsRequestDao.selectLatest();
	}

	@Override
	public int updateStatus(String goodsRequestCode) {
		return purGoodsRequestDao.updateStatus(goodsRequestCode);
	}

}
